<?php
namespace Modules\Admin\Controllers;

class UserController extends \Phalcon\Mvc\Controller {
  public function indexAction() {
    $parameters = [
      'conditions' => [
        'account' => [
          'user_code' => $this -> request -> getQuery('user_code', 'string', '', true),
          'user_level' => $this -> request -> getQuery('user_level', 'int', '', true),
          'shop_level' => $this -> request -> getQuery('shop_level', 'int', '', true)
        ]
      ]
    ];

    $res = $this -> user::getLogicInstance() -> pager($parameters, $this -> request -> getQuery('limit', 'int', 10, true), $this -> request -> getQuery('page', 'int', 1, true));
    if($res['code'] != 0) {
      return ['code'=>$res['code'], 'msg'=>$res['message']];
    }

    return $this -> response -> setJsonContent(['code' => 0,'models' => $res['models'],'thisMonthCreateAccount'=>$this -> user::getInfoServiceInstance() -> count('create_time >= ' . strtotime(date('Y-m-01 00:00:00')))]);
  }

  public function findFirstByIdAction($id) {
    return $this -> response -> setJsonContent($this -> user::getLogicInstance() -> findFirstById($id));
  }

  public function findTeamByUserCodeAction($val) {
    $resUser = $this -> user::getServiceInstance() -> findFirst('user_code = ' . $val);
    if($resUser['code'] != 0) { return $this -> response -> setJsonContent($resUser); }

    return $this -> response -> setJsonContent($this -> user::getServiceInstance() -> find([
      'conditions' => 'promotion_user_id = ' . $resUser['model'] -> user_id,
      'columns' => 'user_id, user_code',
    ]));
  }

  public function findTeamByIdAction($id) {
    return $this -> response -> setJsonContent($this -> user::getServiceInstance() -> find('promotion_user_id = ' . $id));
  }

  public function findFirstByUserCodeAction($val) {
    return $this -> response -> setJsonContent($this -> user::getServiceInstance() -> findFirst([
      'conditions' => 'user_code = ' . $val,
      'columns' => 'user_code, real_name',
    ]));
  }

  public function updateAction($id) {
    $resAccountValidation = $this -> user::getValidationInstance()
    -> addPhone()
    -> addRealName()
    -> addPassword()
    -> addSafePassword()
    -> addUserLevel()
    -> addShopLevel()
    -> addStatus()
    -> execute($this -> request -> getPut('account'));
    if($resAccountValidation['code'] != 0) {
      return $this -> response -> setJsonContent($resAccountValidation);
    }

    $resAccountUpdateById = $this -> user::getServiceInstance() -> updateById($id, $resAccountValidation['data']);
    if($resAccountUpdateById['code'] != 0) {
      return $this -> response -> setJsonContent($resAccountUpdateById);
    }

    $resAccountInfoValidation = $this -> user::getInfoValidationInstance()
    -> addAddressCode()
    -> addAddressDetail()
    -> execute($this -> request -> getPut('account_info'));
    if($resAccountInfoValidation['code'] != 0) {
      return $this -> response -> setJsonContent($resAccountInfoValidation);
    }

    $resAccountInfoUpdateById = $this -> user::getInfoServiceInstance() -> updateById($id, $resAccountInfoValidation['data']);
    if($resAccountInfoUpdateById['code'] != 0) {
      return $this -> response -> setJsonContent($resAccountInfoUpdateById);
    }

    return $this -> response -> setJsonContent(['code'=>0, 'message'=>'更新成功!']);
  }
}
